package com.test.javafaq_portal.controller;


import com.test.javafaq_portal.entity.User;
import com.test.javafaq_portal.service.IUserService;
import com.test.javafaq_portal.vo.R;
import com.test.javafaq_portal.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author test.com
 * @since 2025-04-11
 */
@RestController
@RequestMapping("/v1/users")
public class UserController {

    @Autowired
    IUserService userService;

    @GetMapping("/me")
    public R<UserVo> me(){
        //测试页面： {"code":200,"message":"OK","data":{"id":11,"username":"st2","nickname":"李四同学","questions":21,"collections":0}}
        UserVo userVo = userService.currentUserVo();
        return R.ok(userVo);
    }

    @GetMapping("/teacher")
    public R<List<User>> teacher(){
        List<User> teachers = userService.getTeachers();
        return R.ok(teachers);
    }


    //localhost:8080/v1/users/get?id=1
    @GetMapping("/get")
    @PreAuthorize("hasAuthority('/user/get')") //设置访问这个权限的方法
    public User get(Integer id){
        User u = userService.getById(id);
        return u;
    }
    //localhost:8080/v1/users/list
    @GetMapping("/list")
    @PreAuthorize("hasAuthority('/user/list')")
    public List<User> list(){
        List<User> list = userService.list();
        return list;
    }

}
